package com.sino.uam.support;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS;
import org.apache.commons.dbcp.datasources.SharedPoolDataSource;
import org.apache.log4j.Logger;

public final class UAMDataSource {

    private static DataSource ds;
    private static Logger logger = Logger.getLogger(UAMDataSource.class);

    @SuppressWarnings("unchecked")
    public void init(Properties props) {
        try {
            DriverAdapterCPDS pcds = new DriverAdapterCPDS();
            pcds.setDriver("oracle.jdbc.driver.OracleDriver");
            String url = props.getProperty("uam.jdbc.url");
            String name = props.getProperty("uam.jdbc.username");
            String pwd = props.getProperty("uam.jdbc.password");
            pcds.setUrl(url);
            pcds.setUser(name);
            pcds.setPassword(pwd);
            pcds.setMaxActive(3);
            pcds.setMaxIdle(120);
            SharedPoolDataSource tds = new SharedPoolDataSource();
            tds.setConnectionPoolDataSource(pcds);
            tds.setMaxActive(pcds.getMaxActive());
            tds.setMaxWait(pcds.getMaxIdle());
            String validationQuery = "select 1 from dual";
            if (validationQuery != null)
                tds.setValidationQuery(validationQuery);
            ds = tds;
        } catch (Exception e) {
            logger.error(">>>>>>error connect uam datasource, system can not get user data!!!", e);
        }
    }

    public static DataSource getDataSource() {
        return ds;
    }
}